package com.example.demo1.handle;

import com.example.demo1.entity.Result;
import com.example.demo1.exception.GirlException;
import com.example.demo1.until.ResultUntil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@ControllerAdvice
public class ExceptionHandle {
    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        if (e instanceof GirlException) {
            GirlException girlException = (GirlException) e;
            return ResultUntil.error(girlException.getCode(), girlException.getMessage());
        }
        logger.info("【系统异常】 {}", e);
        return ResultUntil.error(-1, "未知错误");
    }
}
